/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.config.Config;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.object.Mail;
import etcore.webapp.grouptalk.common.service.MailService;
import etcore.webapp.grouptalk.user.object.User;

/**
 * ActivateUserMailUtil is a utility class sending user a mail for activation
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class ActivateUserMailUtil {
	/**
	 * Get a URL string for user activation
	 * 
	 * @param requestUrl
	 *            a string of request URL
	 * @param userName
	 *            target userName
	 * @param userEmail
	 *            user's email
	 * @return
	 */
	public static String getActivationLink(String requestUrl, String userName,
			String userEmail) {
		MD5 md5 = new MD5();

		String code = md5.getMD5ofStr(userEmail.substring(0,
				userEmail.indexOf("@")))
				+ "_"
				+ md5.getMD5ofStr(userEmail.substring(userEmail.indexOf("@") + 1));
		StringBuffer linkBuffer = new StringBuffer();
		int dotIndex = requestUrl.indexOf(Symbols.DOT);

		int slashIndex = requestUrl.length();
		if (dotIndex != -1)
			slashIndex = requestUrl.indexOf(Symbols.SLASH, dotIndex);

		if (slashIndex != -1)
			linkBuffer.append(requestUrl.substring(0, slashIndex));
		else
			linkBuffer.append("..");
		linkBuffer.append("/group/activateUser.action/~");
		String toBeEncrypted = "?cmd=activateUser&userName=" + userName
				+ "&code=" + code;
		String encrypted = SymmetricEncryptionUtil
				.getEncryptedStr(toBeEncrypted);
		linkBuffer.append(encrypted);

		return linkBuffer.toString();

	}

	/**
	 * Send user a mail message which contains user activation URL
	 * 
	 * @param request
	 *            HttpServletRequest instance
	 * @param userName
	 *            target userName
	 * @param userEmail
	 *            target user email
	 */
	public static void sendActivationMail(HttpServletRequest request,
			String userName, String userEmail) {
		HttpSession session = request.getSession(true);

		String lang = (String) session.getAttribute(Messages.LANG);
		StringBuffer titleBuffer = new StringBuffer();
		StringBuffer contentBuffer = new StringBuffer();
		String requestUrl = request.getRequestURL().toString();

		String link = getActivationLink(requestUrl, userName, userEmail);
		String provider = null;

		if (lang != null && lang.equals(Messages.LANG_EN)) {
			provider = Messages.getELabel(Messages.TITLE);
			titleBuffer.append(Messages
					.getELabel(Messages.ACTIVATION_LETTER_TITLE));

			contentBuffer.append(Messages
					.getELabel(Messages.ACTIVATION_LETTER_GREETING));
			contentBuffer.append(Symbols.SPACE);
			contentBuffer.append("<font color=red>");
			contentBuffer.append(userName);
			contentBuffer.append("</font>");
			contentBuffer.append("<br><br><b>");

			contentBuffer.append(Messages
					.getELabel(Messages.ACTIVATION_LETTER_NOTE));
			contentBuffer.append("</b><p align=center><A HREF='");
			contentBuffer.append(link);
			contentBuffer.append("'>");
			contentBuffer.append(link);
			contentBuffer.append("</A></p>");

		} else {
			provider = Messages.getLabel(Messages.TITLE);
			titleBuffer.append(Messages
					.getLabel(Messages.ACTIVATION_LETTER_TITLE));

			contentBuffer.append(Messages
					.getLabel(Messages.ACTIVATION_LETTER_GREETING));
			contentBuffer.append(Symbols.SPACE);
			contentBuffer.append("<font color=red>");
			contentBuffer.append(userName);
			contentBuffer.append("</font>");
			contentBuffer.append("<br><br><b>");

			contentBuffer.append(Messages
					.getLabel(Messages.ACTIVATION_LETTER_NOTE));
			contentBuffer.append("</b><p align=center><A HREF='");
			contentBuffer.append(link);
			contentBuffer.append("'>");
			contentBuffer.append(link);
			contentBuffer.append("</A></p>");
		}

		MailService mailService = new MailService();
		Mail mail = new Mail();
		mail.setName(titleBuffer.toString());
		mail.setContent(contentBuffer.toString());
		mail.setTime(DateUtil.getTime(Config.getProperty(Config.HOURS_DIFFERENCE)));
		mail.setToUsers(userEmail);
		mail.setUserName(User.ROOT);
		mail.setFromUser(provider);
		mailService.save(mail);

		MailSender.sendEmail(mail);
	}
}
